﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Model;
using XT.Repository;

namespace XT.BusinessService
{
    

    public class AccountService : IAccountService
    {
        private readonly IUow _uow;
        public AccountService(IUow uow)
        {
            _uow = uow;
        }


        public Account getAccountById(int id)
        {
            return _uow.AccountRepo.Find(id);
        }

        public int AddAccount(Account accountNeedSave)
        {
            try
            {

                accountNeedSave.Account_Type_Id = 2;
                accountNeedSave.Account_Created_Date = DateTime.Now;
                accountNeedSave.Account_Status = 1;
                accountNeedSave.Account_Info_Permission = 1;
                _uow.AccountRepo.Add(accountNeedSave);
                return 1;

               
            }
            catch
            {
                return -1;
            }
        }

        public Account checkLogin(string mail, string password)
        {
            try
            {
                if (_uow.AccountRepo.checkLogin(mail,password)!=null)
                {
                    Account acc = _uow.AccountRepo.checkLogin(mail, password);
                    return acc;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public int checkExistEmail(string mail)
        {
            try
            {
                if (_uow.AccountRepo.ExecuteQuery(context => context.GetTable<Account>().SingleOrDefault(c => c.Account_Email.Equals(mail))) == null)
                {
                    return 1;
                }
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        public int checkExistUsername(string username)
        {
            try
            {
                if (_uow.AccountRepo.ExecuteQuery(context => context.GetTable<Account>().SingleOrDefault(c => c.Account_Username.Equals(username))) == null)
                {
                    return 1;
                }
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        public Account searchAccountByName(string txt)
        {
            try
            {
                if (_uow.AccountRepo.ExecuteQuery(context => context.GetTable<Account>().SingleOrDefault(c => c.Account_Name.Equals(txt))) != null)
                {
                    Account acc = _uow.AccountRepo.ExecuteQuery(context => context.GetTable<Account>().SingleOrDefault(c => c.Account_Name.Equals(txt)));
                    return acc;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public Account updateAccount(int Id, Account account)
        {
            try
            {
                return _uow.AccountRepo.Save(Id, (context, updating) =>
                {
                    updating.Account_Username = updating.Account_Username;
                    updating.Account_Password = updating.Account_Password;
                    updating.Account_Name = account.Account_Name;
                    updating.Account_Address = account.Account_Address;
                    updating.Account_Phone = account.Account_Phone;
                    updating.Account_Email = account.Account_Email;
                    updating.Account_Facebook = account.Account_Facebook;
                    updating.Account_About = account.Account_About;
                    updating.Account_Type_Id = account.Account_Type_Id;
                    updating.Account_Status = account.Account_Status;
                    updating.Account_Gender = account.Account_Gender;
                    updating.Account_Birthday = account.Account_Birthday;
                    updating.Account_Avatar = updating.Account_Avatar;
                    updating.Account_Cover = updating.Account_Cover;
                    return true;
                });
            }
            catch
            {
                throw new System.Exception();
            }
        }

        public Account UpdatePassword(int Id, Account account)
        {
             try
            {
                return _uow.AccountRepo.Save(Id, (context, updating) =>
                {
                    updating.Account_Password = updating.Account_Password;
                    return true;
                });

             } 
             catch
            {
                throw new System.Exception();
            }
        }
    }
}
